package net.mcreator.playerhuds.procedure;

import net.minecraftforge.fml.common.gameevent.TickEvent;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
import net.minecraftforge.fml.common.event.FMLPreInitializationEvent;
import net.minecraftforge.common.MinecraftForge;

import net.minecraft.world.World;
import net.minecraft.scoreboard.Scoreboard;
import net.minecraft.scoreboard.ScoreObjective;
import net.minecraft.scoreboard.Score;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.Entity;

import net.mcreator.playerhuds.ElementsPlayerhudsMod;

import java.util.Map;

@ElementsPlayerhudsMod.ModElement.Tag
public class ProcedureMainHudUpdate extends ElementsPlayerhudsMod.ModElement {
	public ProcedureMainHudUpdate(ElementsPlayerhudsMod instance) {
		super(instance, 2);
	}

	public static void executeProcedure(Map<String, Object> dependencies) {
		if (dependencies.get("entity") == null) {
			System.err.println("Failed to load dependency entity for procedure MainHudUpdate!");
			return;
		}
		Entity entity = (Entity) dependencies.get("entity");
		entity.getEntityData().setString("HealthNumShow",
				(((new java.text.DecimalFormat("0.#").format(((entity instanceof EntityLivingBase) ? ((EntityLivingBase) entity).getHealth() : -1))))
						+ "" + ("/") + "" + ((new java.text.DecimalFormat("0")
								.format(((entity instanceof EntityLivingBase) ? ((EntityLivingBase) entity).getMaxHealth() : -1))))));
		entity.getEntityData().setDouble("HealthStripUpdate", ((((entity instanceof EntityLivingBase) ? ((EntityLivingBase) entity).getHealth() : -1)
				/ ((entity instanceof EntityLivingBase) ? ((EntityLivingBase) entity).getMaxHealth() : -1)) * 79));
		entity.getEntityData().setDouble("HungerNumTempSave",
				((entity instanceof EntityPlayer) ? ((EntityPlayer) entity).getFoodStats().getFoodLevel() : 0));
		if (((entity.getEntityData().getDouble("HungerNumTempSave")) == 20)) {
			entity.getEntityData().setDouble("HungerStripMoveUpdate", 0);
		} else {
			entity.getEntityData().setDouble("HungerStripMoveUpdate", (80 - ((entity.getEntityData().getDouble("HungerNumTempSave")) * 3.95)));
		}
		entity.getEntityData().setString("HungerNumShow",
				(((new java.text.DecimalFormat("0").format((entity.getEntityData().getDouble("HungerNumTempSave"))))) + "" + ("/") + "" + ("20")));
		entity.getEntityData().setDouble("HungerStripUpdate", (((entity.getEntityData().getDouble("HungerNumTempSave")) / 20) * 79));
		entity.getEntityData().setDouble("ArmorNumSaveTemp",
				((entity instanceof EntityLivingBase) ? ((EntityLivingBase) entity).getTotalArmorValue() : 0));
		if (((entity.getEntityData().getDouble("ArmorNumSaveTemp")) == 20)) {
			entity.getEntityData().setDouble("ArmorStripMoveUpdate", 0);
		} else {
			entity.getEntityData().setDouble("ArmorStripMoveUpdate", (80 - ((entity.getEntityData().getDouble("ArmorNumSaveTemp")) * 3.95)));
		}
		entity.getEntityData().setString("ArmorNumShow",
				(((new java.text.DecimalFormat("0").format((entity.getEntityData().getDouble("ArmorNumSaveTemp"))))) + "" + ("/") + "" + ("20")));
		entity.getEntityData().setDouble("ArmorStripUpdate", (((entity.getEntityData().getDouble("ArmorNumSaveTemp")) / 20) * 79));
		entity.getEntityData().setDouble("AirNumSaveTemp", (new Object() {
			public int getScore(String score) {
				if (entity instanceof EntityPlayer) {
					Scoreboard _sc = ((EntityPlayer) entity).getWorldScoreboard();
					ScoreObjective _so = _sc.getObjective(score);
					if (_so != null) {
						Score _scr = _sc.getOrCreateScore(((EntityPlayer) entity).getGameProfile().getName(), _so);
						return _scr.getScorePoints();
					}
				}
				return 0;
			}
		}.getScore("AirNumSaveTemp")));
		if (((entity.getEntityData().getDouble("AirNumSaveTemp")) == 300)) {
			entity.getEntityData().setDouble("AirStripMoveUpdate", 0);
		} else {
			entity.getEntityData().setDouble("AirStripMoveUpdate", (80 - (((entity.getEntityData().getDouble("AirNumSaveTemp")) / 15) * 3.95)));
		}
		entity.getEntityData().setString("AirNumShow",
				(((new java.text.DecimalFormat("0").format(((entity.getEntityData().getDouble("AirNumSaveTemp")) / 15)))) + "" + ("/") + ""
						+ ("20")));
		entity.getEntityData().setDouble("AirStripUpdate", (((entity.getEntityData().getDouble("AirNumSaveTemp")) / 300) * 79));
	}

	@SubscribeEvent
	public void onPlayerTick(TickEvent.PlayerTickEvent event) {
		if (event.phase == TickEvent.Phase.END) {
			Entity entity = event.player;
			World world = entity.world;
			int i = (int) entity.posX;
			int j = (int) entity.posY;
			int k = (int) entity.posZ;
			java.util.HashMap<String, Object> dependencies = new java.util.HashMap<>();
			dependencies.put("x", i);
			dependencies.put("y", j);
			dependencies.put("z", k);
			dependencies.put("world", world);
			dependencies.put("entity", entity);
			dependencies.put("event", event);
			this.executeProcedure(dependencies);
		}
	}

	@Override
	public void preInit(FMLPreInitializationEvent event) {
		MinecraftForge.EVENT_BUS.register(this);
	}
}
